package com.skyjing.askit

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.graphics.Rect
import android.util.Log
import android.view.View
import android.view.ViewTreeObserver.OnGlobalLayoutListener
import android.view.inputmethod.InputMethodManager
import com.skyjing.askit.AsTool.getApp
import java.util.*

object AsKeyboardTool {
    private const val sDecorViewInvisibleHeightPre = 0
    private val onGlobalLayoutListener: OnGlobalLayoutListener? = null
    private val onSoftInputChangedListener: OnSoftInputChangedListener? = null
    private const val sContentViewInvisibleHeightPre5497 = 0
    /**
     * 显示软键盘
     * @param activity
     */
    @JvmStatic
    fun showSoftInput(activity: Activity) {
        val imm =
            activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
                ?: return
        var view = activity.currentFocus
        if (view == null) {
            view = View(activity)
            view.isFocusable = true
            view.isFocusableInTouchMode = true
            view.requestFocus()
        }
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED)
    }

    @JvmStatic
    fun showSoftInput(view: View) {
        val imm =
            Objects.requireNonNull(getApp())?.getSystemService(
                Context.INPUT_METHOD_SERVICE
            ) as InputMethodManager
        view.isFocusable = true
        view.isFocusableInTouchMode = true
        view.requestFocus()
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED)
    }

    @JvmStatic
    fun hideSoftInput(activity: Activity) {
        val imm =
            activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
                ?: return
        var view = activity.currentFocus
        if (view == null) view = View(activity)
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    @JvmStatic
    fun hideSoftInput(view: View) {
        val imm =
            Objects.requireNonNull(getApp())?.getSystemService(
                Activity.INPUT_METHOD_SERVICE
            ) as InputMethodManager
                ?: return
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }

    /**
     * 开关键盘，没有editview也可以弹出软键盘
     */
    @JvmStatic
    fun showToggleSoftInput() {
        val imm =
            (Objects.requireNonNull(getApp())?.getSystemService(
                Context.INPUT_METHOD_SERVICE
            ) as InputMethodManager)
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
    }

    /**
     * 隐藏软键盘
     * @param activity
     */
    @JvmStatic
    fun hideToggleSoftInput(activity: Activity) {
        if (isSoftInputVisible(activity)) {
            return
        }
        showToggleSoftInput()
    }

    @JvmStatic
    fun isSoftInputVisible(activity: Activity): Boolean {
        return getDecorViewInvisibleHeight(
            activity
        ) > 0
    }

    private var sDecorViewDelta = 0
    private fun getDecorViewInvisibleHeight(activity: Activity): Int {
        val decorView = activity.window.decorView ?: return sDecorViewInvisibleHeightPre
        val outRect = Rect()
        decorView.getWindowVisibleDisplayFrame(outRect)
        Log.e(
            "KeyboardUtils", "getDecorViewInvisibleHeight: "
                    + (decorView.bottom - outRect.bottom)
        )
        val delta = Math.abs(decorView.bottom - outRect.bottom)
        if (delta <= navBarHeight) {
            sDecorViewDelta = delta
            return 0
        }
        return delta - sDecorViewDelta
    }

    private val statusBarHeight: Int
        private get() {
            val resources = Resources.getSystem()
            val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
            return resources.getDimensionPixelSize(resourceId)
        }

    private val navBarHeight: Int
        private get() {
            val res = Resources.getSystem()
            val resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android")
            return if (resourceId != 0) {
                res.getDimensionPixelSize(resourceId)
            } else {
                0
            }
        }

    interface OnSoftInputChangedListener {
        fun onSoftInputChanged(height: Int)
    }
}